define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () 
        {
            //选项卡A元素绑定切换事件
            $('a[data-toggle=tab]').on('shown.bs.tab', function(){
                var panel = $($(this).attr("href"));

                //判断是否有获取到选项的元素
                if(panel.length > 0)
                {
                    //Controller.table.receivce()
                    Controller.table[panel.attr('id')].call(this)

                    //刷新表格
                    $(this).on('click', function (e) 
                    {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }

                //防止元素重复绑定事件
                $(this).unbind('shown.bs.tab');

            })

            //必须默认触发shown.bs.tab事件 一进入就默认触发一个选项卡
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

            Controller.api.bindevent();
        },
        table: { //自定义命名 - 处理选项卡的每个选项的方法
            //客户基本资料
            info()
            {
                
                Controller.api.bindevent();
            },

            //申请记录
            receive()
            {
                 //获取当前的客户ID
                 var ids = Fast.api.query('ids')

                 //初始化表格结构
                 Table.api.init({
                    extend: {
                        ReceiveIndex_url: `business/info/receive?ids=${ids}`,
                        ReceiveAdd_url: `business/receive/add?ids=${ids}`,
                        ReceiveEdit_url: `business/receive/edit`,
                        ReceiveDel_url: `business/receive/del`,
                        table: 'business_receive',
                    }
                });

                //获取表格元素
                var ReceiveTable = $('#ReceiveTable')

                ReceiveTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.ReceiveIndex_url,
                    toolbar: "#ReceiveToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'applytime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [
                            { checkbox: true },
                            { field: ('admin.username'), title: __('申请人'), sortable: true, operate:false },
                            { 
                                field: 'applytime',   
                                title:('申请时间'), 
                                formatter: Table.api.formatter.datetime, 
                                operate: 'RANGE', 
                                addclass: 'datetimerange', 
                                sortable: true
                            },
                            { field: 'business.nickname', title: __('接收人'), operate: 'LIKE'},
                            { 
                                field: 'status_text', 
                                title: ('申请状态'), 
                                operate: 'LIKE' 
                            },
                            {
                                field: 'operate',
                                title: ('操作'),
                                table: ReceiveTable,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate,
                                buttons: [
                                    { 
                                        name: 'ReceiveEdit',
                                        title: '编辑',
                                        icon: 'fa fa-pencil',
                                        classname: 'btn btn-xs btn-success btn-dialog',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                        url: $.fn.bootstrapTable.defaults.extend.ReceiveEdit_url,
                                    },
                                    {
                                        name: 'ReceiveDel',
                                        title: '删除',
                                        icon: 'fa fa-trash',
                                        classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                        url: $.fn.bootstrapTable.defaults.extend.ReceiveDel_url,
                                        confirm: '是否确认删除？',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                        success:function(data,ret)
                                        {
                                            ReceiveTable.bootstrapTable('refresh');
                                        }
                                    },

                                ]
                            }
                        ]
                    ]
                });

                //给添加按钮绑定事件，打开一个新的窗口
                $(".btn-receive-add").on('click', function(){
                    Fast.api.open($.fn.bootstrapTable.defaults.extend.ReceiveAdd_url, '添加领取记录', {area:['65%','65%']})
                })

                //表格渲染
                Table.api.bindevent(ReceiveTable);
            },
            //回访记录
            visit()
            {
                //获取当前的客户ID
                var ids = Fast.api.query('ids')

                //初始化表格结构
                Table.api.init({
                    extend: {
                        VisitIndex_url: `business/info/visit?ids=${ids}`,
                        VisitAdd_url: `business/visit/add?ids=${ids}`,
                        VisitEdit_url: `business/visit/edit`,
                        VisitDel_url: `business/visit/del`,
                        table: 'business_visit',
                    }
                });

                //获取表格元素
                var VisitTable = $('#VisitTable')

                VisitTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.VisitIndex_url,
                    toolbar: "#VisitToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true, operate:false },
                            { 
                                field: 'createtime', 
                                title: __('VisitTime'), 
                                formatter: Table.api.formatter.datetime, 
                                operate: 'RANGE', 
                                addclass: 'datetimerange', 
                                sortable: true
                            },
                            { field: 'admin.nickname', title: __('BusinessAdmin'), operate: 'LIKE'},
                            { 
                                field: 'content', 
                                title: __('VisitContent'), 
                                operate: 'LIKE' 
                            },
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: VisitTable,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate,
                                buttons: [
                                    { 
                                        name: 'VisitEdit',
                                        title: '编辑',
                                        icon: 'fa fa-pencil',
                                        classname: 'btn btn-xs btn-success btn-dialog',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                        url: $.fn.bootstrapTable.defaults.extend.VisitEdit_url,
                                    },
                                    {
                                        name: 'VisitDel',
                                        title: '删除',
                                        icon: 'fa fa-trash',
                                        classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                        url: $.fn.bootstrapTable.defaults.extend.VisitDel_url,
                                        confirm: '是否确认删除？',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                        success:function(data,ret)
                                        {
                                            VisitTable.bootstrapTable('refresh');
                                        }
                                    },

                                ]
                            }
                        ]
                    ]
                });

                //给添加按钮绑定事件，打开一个新的窗口
                $(".btn-visit-add").on('click', function(){
                    Fast.api.open($.fn.bootstrapTable.defaults.extend.VisitAdd_url, '添加回访记录', {area:['65%','65%']})
                })

                //表格渲染
                Table.api.bindevent(VisitTable);
            },
            //消费记录
            record()
            {
                //获取当前的客户ID
                var ids = Fast.api.query('ids')

                 //初始化表格结构
                 Table.api.init({
                    extend: {
                        RecordIndex_url: `business/info/record?ids=${ids}`,
                        RecordAdd_url: `business/record/add?ids=${ids}`,
                        RecordEdit_url: `business/record/edit`,
                        RecordDel_url: `business/record/del`,
                        table: 'business_record',
                    }
                });

                //获取表格元素
                var RecordTable = $('#RecordTable')

                RecordTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.RecordIndex_url,
                    toolbar: "#RecordToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [
                            { checkbox: true },
                            { field: ('total'), title: __('消费金额￥'), sortable: true, operate:false },
                            { 
                                field: 'createtime', 
                                title:('消费时间'), 
                                formatter: Table.api.formatter.datetime, 
                                operate: 'RANGE', 
                                addclass: 'datetimerange', 
                                sortable: true
                            },
                            { field: 'business.nickname', title: __('购买人'), operate: 'LIKE'},
                            { 
                                field: 'content', 
                                title: ('描述'), 
                                operate: 'LIKE' 
                            },
                            {
                                field: 'operate',
                                title: ('操作'),
                                table: RecordTable,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate,
                                buttons: [
                                    { 
                                        name: 'RecordEdit',
                                        title: '编辑',
                                        icon: 'fa fa-pencil',
                                        classname: 'btn btn-xs btn-success btn-dialog',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                        url: $.fn.bootstrapTable.defaults.extend.RecordEdit_url,
                                    },
                                    {
                                        name: 'RecordDel',
                                        title: '删除',
                                        icon: 'fa fa-trash',
                                        classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                        url: $.fn.bootstrapTable.defaults.extend.RecordDel_url,
                                        confirm: '是否确认删除？',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                        success:function(data,ret)
                                        {
                                            RecordTable.bootstrapTable('refresh');
                                        }
                                    },

                                ]
                            }
                        ]
                    ]
                });

                //给添加按钮绑定事件，打开一个新的窗口
                $(".btn-record-add").on('click', function(){
                    Fast.api.open($.fn.bootstrapTable.defaults.extend.RecordAdd_url, '添加消费记录', {area:['65%','65%']})
                })

                //表格渲染
                Table.api.bindevent(RecordTable);
            },
            //课程订单
            order()
            {
                //获取当前的客户ID
                var ids = Fast.api.query('ids')

                 //初始化表格结构
                 Table.api.init({
                    extend: {
                        OrderIndex_url: `business/info/Order?ids=${ids}`,
                        OrderAdd_url: `business/order/add?ids=${ids}`,
                        OrderEdit_url: `business/order/edit`,
                        OrderDel_url: `business/order/del`,
                        table: 'business_order',
                    }
                });

                //获取表格元素
                var OrderTable = $('#OrderTable')

                OrderTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.OrderIndex_url,
                    toolbar: "#OrderToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [
                            { checkbox: true },
                            { 
                                field: 'code', 
                                title: ('订单号'), 
                                operate: 'LIKE' 
                            },
                            { field: ('total'), title: __('订单金额￥'), sortable: true, operate:false },
                            { 
                                field: 'createtime', 
                                title:('下单时间'), 
                                formatter: Table.api.formatter.datetime, 
                                operate: 'RANGE', 
                                addclass: 'datetimerange', 
                                sortable: true
                            },
                            { field: 'business.nickname', title: __('订单用户'), operate: 'LIKE'},
                            { 
                                field: 'pay_text', 
                                title: ('支付方式'), 
                                operate: 'LIKE' 
                            },
                            {
                                field: 'operate',
                                title: ('操作'),
                                table: OrderTable,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate,
                                buttons: [
                                    { 
                                        name: 'RecordEdit',
                                        title: '编辑',
                                        icon: 'fa fa-pencil',
                                        classname: 'btn btn-xs btn-success btn-dialog',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                        url: $.fn.bootstrapTable.defaults.extend.OrderEdit_url,
                                    },
                                    {
                                        name: 'RecordDel',
                                        title: '删除',
                                        icon: 'fa fa-trash',
                                        classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                        url: $.fn.bootstrapTable.defaults.extend.OrderDel_url,
                                        confirm: '是否确认删除？',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                        success:function(data,ret)
                                        {
                                            OrderTable.bootstrapTable('refresh');
                                        }
                                    },

                                ]
                            }
                        ]
                    ]
                });

                //给添加按钮绑定事件，打开一个新的窗口
                $(".btn-order-add").on('click', function(){
                    Fast.api.open($.fn.bootstrapTable.defaults.extend.OrderAdd_url, '添加课程订单', {area:['65%','65%']})
                })

                //表格渲染
                Table.api.bindevent(OrderTable);
            },
            //评论记录
            comment()
            {
               //获取当前的客户ID
               var ids = Fast.api.query('ids')

               //初始化表格结构
               Table.api.init({
                  extend: {
                      CommentIndex_url: `business/info/comment?ids=${ids}`,
                      CommentAdd_url: `business/comment/add?ids=${ids}`,
                      CommentEdit_url: `business/comment/edit`,
                      CommentDel_url: `business/comment/del`,
                      table: 'business_comment',
                  }
              });

              //获取表格元素
              var CommentTable = $('#CommentTable')

              CommentTable.bootstrapTable({
                  url: $.fn.bootstrapTable.defaults.extend.CommentIndex_url,
                  toolbar: "#CommentToolbar", //工具栏
                  pk: 'id', //默认主键字段名
                  sortName: 'createtime', //排序的字段名
                  sortOrder: 'desc', //排序的方式
                  columns: [
                      [
                          { checkbox: true },
                          { field: ('business.nickname'), title: __('评论用户'), sortable: true, operate:false },
                          { 
                              field: 'createtime',   
                              title:('评论时间'), 
                              formatter: Table.api.formatter.datetime, 
                              operate: 'RANGE', 
                              addclass: 'datetimerange', 
                              sortable: true
                          },
                          { field: 'content', title: __('评论内容'), operate: 'LIKE'},
                          { 
                              field: 'subject.title', 
                              title: ('评论课程'), 
                              operate: 'LIKE' 
                          },
                          {
                              field: 'operate',
                              title: ('操作'),
                              table: CommentTable,
                              events: Table.api.events.operate,
                              formatter: Table.api.formatter.operate,
                              buttons: [
                                  { 
                                      name: 'CommentEdit',
                                      title: '编辑',
                                      icon: 'fa fa-pencil',
                                      classname: 'btn btn-xs btn-success btn-dialog',
                                      extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                      url: $.fn.bootstrapTable.defaults.extend.CommentEdit_url,
                                  },
                                  {
                                      name: 'CommentDel',
                                      title: '删除',
                                      icon: 'fa fa-trash',
                                      classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                      url: $.fn.bootstrapTable.defaults.extend.CommentDel_url,
                                      confirm: '是否确认删除？',
                                      extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                      success:function(data,ret)
                                      {
                                        CommentTable.bootstrapTable('refresh');
                                      }
                                  },

                              ]
                          }
                      ]
                  ]
              });

              //给添加按钮绑定事件，打开一个新的窗口
              $(".btn-comment-add").on('click', function(){
                  Fast.api.open($.fn.bootstrapTable.defaults.extend.CommentAdd_url, '添加领取记录', {area:['65%','65%']})
              })

              //表格渲染
              Table.api.bindevent(CommentTable);
            },
            //佣金提现记录
            commission()
            {   
               
                //获取当前的客户ID
               var ids = Fast.api.query('ids')

               //初始化表格结构
               Table.api.init({
                  extend: {
                      CommissionIndex_url: `business/info/commission?ids=${ids}`,
                      CommissionAdd_url: `business/commission/add?ids=${ids}`,
                      CommissionEdit_url: `business/commission/edit`,
                      CommissionDel_url: `business/commission/del`,
                      table: 'business_commission',
                  }
              });

              //获取表格元素
              var CommissionTable = $('#CommissionTable')

              CommissionTable.bootstrapTable({
                  url: $.fn.bootstrapTable.defaults.extend.CommissionIndex_url,
                  toolbar: "#CommissionToolbar", //工具栏
                  pk: 'id', //默认主键字段名
                  sortName: 'createtime', //排序的字段名
                  sortOrder: 'desc', //排序的方式
                  columns: [
                      [
                          { checkbox: true },
                          { field: ('business.nickname'), title: __('消费用户'), sortable: true, operate:false },
                          { 
                              field: 'createtime',   
                              title:('消费时间'), 
                              formatter: Table.api.formatter.datetime, 
                              operate: 'RANGE', 
                              addclass: 'datetimerange', 
                              sortable: true
                          },
                          { field: 'amount', title: __('可提现佣金￥'), operate: 'LIKE'},
                          { 
                              field: 'parent.nickname', 
                              title: ('推荐人'), 
                              operate: 'LIKE' 
                          },
                          { 
                            field: 'status_text', 
                            title: ('提现状态'), 
                            operate: 'LIKE' 
                        },
                          {
                              field: 'operate',
                              title: ('操作'),
                              table: CommissionTable,
                              events: Table.api.events.operate,
                              formatter: Table.api.formatter.operate,
                              buttons: [
                                  { 
                                      name: 'CommissionEdit',
                                      title: '编辑',
                                      icon: 'fa fa-pencil',
                                      classname: 'btn btn-xs btn-success btn-dialog',
                                      extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                      url: $.fn.bootstrapTable.defaults.extend.CommentEdit_url,
                                  },
                                  {
                                      name: 'CommissionDel',
                                      title: '删除',
                                      icon: 'fa fa-trash',
                                      classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                      url: $.fn.bootstrapTable.defaults.extend.CommissionDel_url,
                                      confirm: '是否确认删除？',
                                      extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                      success:function(data,ret)
                                      {
                                        CommissionTable.bootstrapTable('refresh');
                                      }
                                  },

                              ]
                          }
                      ]
                  ]
              });

              //给添加按钮绑定事件，打开一个新的窗口
              $(".btn-commission-add").on('click', function(){
                  Fast.api.open($.fn.bootstrapTable.defaults.extend.CommissionAdd_url, '添加领取记录', {area:['65%','65%']})
              })

              //表格渲染
              Table.api.bindevent(CommissionTable);
            },
            //优惠券领取记录
            coupon()
            {   
                console.log('优惠券领取记录')
                //获取当前的客户ID
               var ids = Fast.api.query('ids')

               //初始化表格结构
               Table.api.init({
                  extend: {
                    CouponIndex_url: `business/info/coupon?ids=${ids}`,
                    CouponAdd_url: `business/coupon/add?ids=${ids}`,
                    CouponEdit_url: `business/coupon/edit`,
                    CouponDel_url: `business/coupon/del`,
                      table: 'business_coupon',
                  }
              });

              //获取表格元素
              var CouponTable = $('#CouponTable')

              CouponTable.bootstrapTable({
                  url: $.fn.bootstrapTable.defaults.extend.CouponIndex_url,
                  toolbar: "#CouponToolbar", //工具栏
                  pk: 'id', //默认主键字段名
                  sortName: 'createtime', //排序的字段名
                  sortOrder: 'desc', //排序的方式
                  columns: [
                      [
                          { checkbox: true },
                          { field: ('business.nickname'), title: __('领取用户'), sortable: true, operate:false },
                          { 
                              field: 'createtime',   
                              title:('领取时间'), 
                              formatter: Table.api.formatter.datetime, 
                              operate: 'RANGE', 
                              addclass: 'datetimerange', 
                              sortable: true
                          },
                          { field: 'coupon.title', title: __('优惠券标题'), operate: 'LIKE'},
                          { 
                            field: 'status_text', 
                            title: ('优惠券使用状态'), 
                            operate: 'LIKE' 
                        },
                        { 
                            field: 'coupon.total', 
                            title: ('优惠券总数'), 
                            operate: 'LIKE' 
                        },
                          {
                              field: 'operate',
                              title: ('操作'),
                              table: CouponTable,
                              events: Table.api.events.operate,
                              formatter: Table.api.formatter.operate,
                              buttons: [
                                  { 
                                      name: 'CouponEdit',
                                      title: '编辑',
                                      icon: 'fa fa-pencil',
                                      classname: 'btn btn-xs btn-success btn-dialog',
                                      extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                      url: $.fn.bootstrapTable.defaults.extend.CouponEdit_url,
                                  },
                                  {
                                      name: 'CouponDel',
                                      title: '删除',
                                      icon: 'fa fa-trash',
                                      classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                      url: $.fn.bootstrapTable.defaults.extend.CouponDel_url,
                                      confirm: '是否确认删除？',
                                      extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                      success:function(data,ret)
                                      {
                                        CouponTable.bootstrapTable('refresh');
                                      }
                                  },

                              ]
                          }
                      ]
                  ]
              });

              //给添加按钮绑定事件，打开一个新的窗口
              $(".btn-coupon-add").on('click', function(){
                  Fast.api.open($.fn.bootstrapTable.defaults.extend.CouponAdd_url, '添加优惠券领取记录', {area:['65%','65%']})
              })

              //表格渲染
              Table.api.bindevent(CouponTable);
            }
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
